#!/bin/bash
set -e

# 下载镜像
kubeadm config images pull \
  --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers \
  --kubernetes-version v1.22.6

# 配置 hosts
# /etc/hosts
#    192.168.1.189   lazycat.cloud
# 确保预备的 control-plane-endpoint 域名指向 apiserver-advertise-address

# 确认 cni 配置
# /etc/cni/net.d
#   10-k8s.conf
#      需要修改 subnet 为节点所需的子网
#   99-loopback.conf

# 安装集群
kubeadm init \
  --node-name office-189 \
  --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers \
  --control-plane-endpoint lazycat.cloud:6443 \
  --service-cidr 10.96.0.0/16 \
  --pod-network-cidr 10.244.0.0/16 \
  --apiserver-advertise-address 192.168.1.189 \
  --kubernetes-version v1.22.6

# 配置 /etc/kubernetes/manifests/kube-controller-manager.yaml
#   添加参数 --node-cidr-mask-size=16

# 配置 kubectl
install -Dm600 /etc/kubernetes/admin.conf ~/.kube/config
chown $(id -u):$(id -g) ~/.kube/config

# 查看 pod 状态
kubectl -n kube-system get pods

# 配置主节点可调度
kubectl taint node office-189 node-role.kubernetes.io/master-

# 安装 fannel
git clone https://github.com/flannel-io/flannel.git
cd flannel
# 配置 Documentation/kube-flannel.yml
#    为 DaemonSet 的容器加参数 --iface-regex=10.147.18.* 确保监听正确的内网ip
kubectl apply -f Documentation/kube-flannel.yml
kubectl -n kube-system get daemonset

# 安装 nginx-ingress
git clone https://github.com/kubernetes/ingress-nginx.git
cd ingress-nginx
git checkout controller-v1.1.0
sed -i \
  -e 's/k8s.gcr.io\/ingress-nginx\/controller/registry.cn-hangzhou.aliyuncs.com\/google_containers\/nginx-ingress-controller/g' \
  -e 's/k8s.gcr.io\/ingress-nginx\/kube-webhook-certgen/registry.cn-hangzhou.aliyuncs.com\/google_containers\/kube-webhook-certgen/g' \
  deploy/static/provider/cloud/deploy.yaml
kubectl apply -f deploy/static/provider/cloud/deploy.yaml

# 安装 Dashboard v3
kubectl apply -f https://addons.kuboard.cn/kuboard/kuboard-v3.yaml

# 配置 nginx-ingress 的 service 的 externalIP
# 配置 nginx-ingress 的暴露端口
#   https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/
#   --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
#   --udp-services-configmap=$(POD_NAMESPACE)/udp-services
# 配置 nginx-ingress 的代理最大 body size
#   https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#proxy-body-size

# 安装 cert-manager
curl -OL https://github.com/cert-manager/cert-manager/releases/download/v1.7.1/cert-manager.yaml
kubectl apply -f cert-manager.yaml

# 配置 cert-manager
# https://cert-manager.io/docs/usage/ingress/#optional-configuration

# 配置 Dashboard 的 https

# 确保 mount.nfs 命令存在
mount.nfs --version


